package com.magicmovie.api.controller;

import com.google.common.collect.Maps;
import com.magicmovie.api.service.DictionaryService;
import com.magicmovie.api.service.MovieService;
import com.magicmovie.api.service.OrderService;
import com.magicmovie.api.service.ViewHistoryService;
import com.magicmovie.entity.Customer;
import com.magicmovie.entity.Movie;
import com.magicmovie.entity.Order;
import com.magicmovie.entity.ViewHistory;
import com.magicmovie.shiro.CurrentCustomerService;
import com.magicmovie.util.common.AjaxMsg;
import com.magicmovie.util.common.ValidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private CurrentCustomerService currentCustomerService;
    @Autowired
    private ViewHistoryService viewHistoryService;

    @RequestMapping(value = "/movie/list",method = RequestMethod.POST)
    public AjaxMsg list(@RequestParam(required = false)String name, @RequestParam(required = false)String type, @RequestParam(required = false)String area, @RequestParam(defaultValue = "00")String sort, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> params = Maps.newHashMap();
        params.put("sort",sort);
        if(!ValidUtil.isEmpty(name)) params.put("name",name);
        if(!ValidUtil.isEmpty(type)) params.put("type",type);
        if(!ValidUtil.isEmpty(area)) params.put("area",area);
        params.put("status","00");

        Map<String,Object> result = movieService.findByParams(params,pageNum,pageSize);
        return new AjaxMsg(result);
    }

    @RequestMapping(value = "/movie/details",method = RequestMethod.GET)
    public AjaxMsg details(Integer id,@RequestParam(required = false) Integer uid){
        Movie movie = movieService.selectByPrimaryKey(id);
        if("01".equals(movie.getStatus()))
            return new AjaxMsg(AjaxMsg.FAILURE,"该影片已下架！");
        Map<String,Object> map = movieService.findById(id);
        if(uid == null){
            map.put("url","");
            map.put("isPurchased","00");
        }else{
            Order order = orderService.findByUidAndMovieId(uid,id);
            if(movie.getPrice() == 0){
                map.put("isPurchased","01");
            }else{
                if(order == null){
                    map.put("isPurchased","00");
                    map.put("url","");
                } else {
                    map.put("isPurchased","01");
                }
            }
        }
        return new AjaxMsg(map);
    }

    @RequestMapping(value = "/movie/movieClassify",method = RequestMethod.GET)
    public AjaxMsg getMovieClassify(){
        String keyNo = "MOVIE_AREA";
        List<Map<String, Object>> areas = dictionaryService.findByKeyNo(keyNo);
        keyNo = "MOVIE_TYPE";
        List<Map<String, Object>> type = dictionaryService.findByKeyNo(keyNo);

        HashMap<Object, Object> result = Maps.newHashMap();
        result.put("areas",areas);
        result.put("type",type);
        return new AjaxMsg(result);
    }

    @RequestMapping(value = "/movie/addViewHistory",method = RequestMethod.GET)
    public AjaxMsg addViewHistory(Integer movieId){
        if(ValidUtil.isEmpty(movieId))
            return new AjaxMsg(AjaxMsg.FAILURE,"参数缺失！");

        Customer customer = currentCustomerService.getCurrentCustomer();
        ViewHistory viewHistory = new ViewHistory();
        viewHistory.setCustomerId(customer.getId());
        viewHistory.setMovieId(movieId);
        viewHistoryService.insert(viewHistory);
        return new AjaxMsg();
    }

    @RequestMapping(value = "/movie/historyList",method = RequestMethod.GET)
    public AjaxMsg historyList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        Customer customer = currentCustomerService.getCurrentCustomer();
        Map<String, Object> result = viewHistoryService.findByCustomerId(customer.getId(), pageNum, pageSize);
        return new AjaxMsg(result);
    }

    @RequestMapping(value = "/movie/purchasedList",method = RequestMethod.GET)
    public AjaxMsg purchasedList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        Customer customer = currentCustomerService.getCurrentCustomer();
        Map<String, Object> result = movieService.findPurchasedMovie(customer.getId(), pageNum, pageSize);
        return new AjaxMsg(result);
    }

    @RequestMapping(value = "/movie/recommendList",method = RequestMethod.GET)
    public AjaxMsg recommendList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "3") Integer pageSize){
        Map<String, Object> result =  movieService.findByHotSale(pageNum,pageSize);
        return new AjaxMsg(result);
    }
}
